<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lessons extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		// get all of the users lessons for courses subscribed
	}
	public function code_lesson($lesson_id = 0)
	{
		$this->load->model('lesson');
		$this->load->model('user');
		$data['lesson'] = $this->lesson->get_lesson_info($lesson_id);

		$data['logged_in'] = $this->user->is_logged_in();
		if($this->user->is_logged_in())
		{
			$user_id = $this->user->get_id();
			$directory =  'lessons/'.$lesson_id.'/'.$user_id;
			$data['directory'] = $directory;
			$data['lesson_parts'] = $this->lesson->get_lesson_parts($lesson_id);
			$data['lesson_parts_root'] = '../../../lessons/'.$data['lesson']->id.'/lesson_parts/';
			if( !is_dir($directory) )
			{
				mkdir($directory);
			}
			//$data['files'] = scandir($directory);

			$data['files'] = array(); 
			if ($handle = opendir($directory)) {

			    while (false !== ($entry = readdir($handle))) {

			        if ($entry != "." && $entry != "..") {

			            array_push($data['files'], $entry);
			        }
			    }

			    closedir($handle);
			}
		}
		$this->load->view('lessons/lesson', $data);
	}


	public function edit_lesson($lesson_id)
	{
		$this->load->model('lesson');
		$this->load->model('user');
		$user_id = $this->user->get_id();
		if($this->lesson->user_can_edit($lesson_id, $user_id))
		{	
			
			$lesson_parts =  $this->lesson->get_lesson_parts($lesson_id);
			$fileUrl =  'lessons/'.$lesson_id.'/lesson_parts/'.$lesson_parts[0]->file_name;
			$data['first_part_code'] = file_get_contents($fileUrl);
			$data['lesson_parts'] = $lesson_parts;
			
			$data['lesson'] = $this->lesson->get_lesson_info($lesson_id);
		 	$this->load->view('lessons/edit_lesson', $data);
		}
		
	}
	public function save_part_file()
	{
		$lesson = $_POST['lesson'];
		$file = $_POST['file'];
		$fileContents =  $_POST['fileContents'];
		$fileUrl =  'lessons/'.$lesson.'/lesson_parts/'.$file;
		file_put_contents($fileUrl, $fileContents);
	}
	public function get_file()
	{
		$this->load->model('user');
		$lesson = $_POST['lesson'];
		$userId =  $this->user->get_id();
		$fileName = $_POST['file'];
		$fileUrl =  'lessons/'.$lesson.'/'.$userId.'/'.$fileName;
		$file =  file_get_contents($fileUrl);

		echo $file;
		
	}
	public function save_file()
	{
		$this->load->model('user');
		$lesson = $_POST['lesson'];
		$userId = $this->user->get_id();
		$fileName = $_POST['file'];
		$fileContents =  $_POST['fileContents'];
		$fileUrl = $fileUrl =  'lessons/'.$lesson.'/'.$userId.'/'.$fileName;
		$success = file_put_contents($fileUrl, $fileContents);
		if(!$success)
		{
			echo "Something went wrong with the file save";
		}
	}
	public function create_file()
	{
		$this->load->model('user');
		$lesson = $_POST['lesson'];
		$userId = $this->user->get_id();
		$fileName = $_POST['file'];
		$fileContents =  $_POST['fileContents'];
		$fileUrl = $fileUrl =  'lessons/'.$lesson.'/'.$userId.'/'.$fileName;
		$success = file_put_contents($fileUrl, '');
		if(!$success)
		{
			echo "Something went wrong with the file save";
		}
	}
	public function get_lesson_part($lesson_id, $file)
	{
		$this->load->model('lesson');
		$data['lesson'] = $this->lesson->get_lesson_info($lesson_id);
		$fileUrl =  'lessons/'.$lesson_id.'/lesson_parts/'.$file;
		$data['lesson_part'] = file_get_contents($fileUrl);
		$this->load->view('lessons/lesson_part', $data);
	} 
	public function get_part_file_code()
	{
		$lesson = $_POST['lesson'];
		$file = $_POST['file'];
		$fileUrl =  'lessons/'.$lesson.'/lesson_parts/'.$file;
		echo file_get_contents($fileUrl);
	} 
	public function save_part_info()
	{
		$this->load->model('lesson');

		$this->lesson->save_part_info($_POST['part_id'], 
									  $_POST['name'], 
									  $_POST['youtubeLink'], 
									  $_POST['youtubeStart'], 
									  $_POST['youtubeEnd']);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */